package main

import (
    "database/sql"
    "fmt"
    "log"

     "github.com/go-sql-driver/mysql"
)

type Album struct{
    ID int64
    Title string
    Artist string
    Price float32
}
var db *sql.DB
func albumsByArtist(name string)([]Album ,error){
    var albums[]Album

    rows,err:=db.Query("select * from album where artist = ?",name)
    if err !=nil{
        return nil,fmt.Errorf("albumsByArtist %q: %v",name,err)
    }


    defer rows.Close()

    for rows.Next(){
        var alb Album
        if err:=rows.Scan(&alb.ID,&alb.Title,&alb.Artist,&alb.Price);err !=nil{
            return nil,fmt.Errorf("albumsByArtist %q,: %v",name,err)
        }

        albums=append(albums,alb)
    }

    if err:=rows.Err();err !=nil{
        return nil,fmt.Errorf("albumsByArtist %q: %v",name,err)
    }

    return albums,nil
    
}


func albumByID(id int64)(Album ,error){
    var alb Album

    row:=db.QueryRow("select * from album where id = ?",id)

    if err:=row.Scan(&alb.ID,&alb.Title,&alb.Artist,&alb.Price);err!=nil{
        if err==sql.ErrNoRows{
            return alb,fmt.Errorf("albumsById %d:no such album",id)
        }

        return alb,fmt.Errorf("albumsById %d: %v",id,err)
    }



    return alb,nil
}

func addAlbum(alb Album)(int64 ,error){
    result,err:=db.Exec("insert into album(title,artist,price) values(?,?,?)",alb.Title,alb.Artist,alb.Price)

    if err !=nil{

        return 0,fmt.Errorf("addAlbum: %v",err)
    }

    id,err:=result.LastInsertId()

    if err !=nil{
        return 0,fmt.Errorf("addAlbum: %v",err)
    }

    return id,nil

}

func main() {
    // Capture connection properties.
    cfg := mysql.NewConfig()
    cfg.User = "ecs-user"
    cfg.Passwd ="Xmx941512388"
    cfg.Net = "tcp"
    cfg.Addr = "127.0.0.1:3306"
    cfg.DBName = "recordings"
   
    // Get a database handle.
    var err error
    db, err = sql.Open("mysql", cfg.FormatDSN())
    if err != nil {
        log.Fatal(err)
    }

    pingErr := db.Ping()
    if pingErr != nil {
        log.Fatal(pingErr)
    }
    fmt.Println("Connected!")
    // albums, err := albumsByArtist("John Coltrane")
    // if err != nil {
    //     log.Fatal(err)
    // }
    // fmt.Printf("Albums found: %v\n", albums)
    // alb,err:=albumByID(2)


    // if err !=nil{
    //     log.Fatal(err)
    // }

    // fmt.Printf("Album found: %v\n",alb)
    albID, err := addAlbum(Album{
        Title:  "The Modern Sound of Betty Carter",
        Artist: "Betty Carter",
        Price:  49.99,
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("ID of added album: %v\n", albID)
}